package com.yyfax.ph.common.scan.bankcard.activity;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.yyfax.common.CLog;
import com.yyfax.ph.R;

import java.lang.ref.WeakReference;

public class OverlayView extends View {
    private final static String TAG = OverlayView.class.getSimpleName();
    private WeakReference<CardRecoActivity> mScanActivityRef;
    private Rect mCameraPreviewRect;
    ////guide line
    private final static int[] SCANNER_ALPHA = {0, 64, 128, 192, 255, 192, 128, 64};
    private final static long ANIMATION_DELAY = 10L;
    private Context mContext;

    private Paint paint;
    private int frameColor;
    private int lineStep = 0;
    private Rect mLineRect;
    private Drawable lineDrawable;
    private int LINE_HEIGHT = 0;
    private int LINE_SPEED = 5;

    public OverlayView(Context context) {
        super(context);
        setWillNotDraw(false);
        this.mContext = context;
        init();
    }

    public OverlayView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
        this.mContext = context;
        init();
    }

    public OverlayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setWillNotDraw(false);
        this.mContext = context;
        init();
    }

    public void setmActivity(CardRecoActivity captureActivity) {
        mScanActivityRef = new WeakReference<CardRecoActivity>(captureActivity);
    }

    public void init() {
        CLog.i("", "overlayview init");
        paint = new Paint();
        Resources resources = getResources();
        frameColor = resources.getColor(R.color.color_2b92ec);//resources.getColor(ViewUtil.getResourseIdByName(context.getPackageName(), "color", "color_viewfinder_frame"));
        mLineRect = new Rect();
        int lineDrawableId = R.drawable.scan_line_landscape;
        lineDrawable = mContext.getResources().getDrawable(lineDrawableId);
        LINE_HEIGHT = lineDrawable.getIntrinsicHeight();
        //scannerAlpha = 4;
    }

    @Override
    public void onDraw(Canvas canvas) {
        if (mCameraPreviewRect == null) {
            return;
        }
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        if ((lineStep += LINE_SPEED) < height - LINE_HEIGHT) {
            canvas.save();
            mLineRect.set(0, lineStep, width, lineStep + LINE_HEIGHT);
            lineDrawable.setBounds(mLineRect);
            lineDrawable.draw(canvas);
            canvas.restore();
        } else {
            lineStep = 0;
        }
        int lineWidth = 5;
        int roundWidth = 60;
        canvas.save();
        paint.setColor(frameColor);
        canvas.drawRect(0, 0, roundWidth, lineWidth, paint);
        canvas.drawRect(0, 0, lineWidth, roundWidth, paint);
        canvas.drawRect(width - roundWidth, 0, width, lineWidth, paint);
        canvas.drawRect(width - lineWidth, 0, width, roundWidth, paint);
        canvas.drawRect(0, height - lineWidth, roundWidth, height, paint);
        canvas.drawRect(0, height - roundWidth, lineWidth, height, paint);
        canvas.drawRect(width - roundWidth, height - lineWidth, width, height, paint);
        canvas.drawRect(width - lineWidth, height - roundWidth, width, height, paint);
        canvas.restore();
        postInvalidateDelayed(ANIMATION_DELAY);
    }

    public void setCameraPreviewRect(Rect rect) {
        mCameraPreviewRect = rect;
        postInvalidate();
    }
}